<?php
namespace App\Http\ViewComposers;

use Illuminate\View\View;
use Illuminate\Support\Facades\Route;

/**
 * 后台菜单-视图合成器
 * @package App\Http\ViewComposers
 */
class AdminMenuComposer
{
    private $permSer;
    private $menuSer;
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->permSer = new \App\Services\SysPermissionService();
        $this->menuSer = new \App\Services\SysMenuService();
    }
    /**
     * 将数据绑定到视图。
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {
        //当前焦点菜单，控制左侧选中菜单路由名称
        $curRouteName = Route::currentRouteName();
        //当前权限数据
        $curPermData = $this->permSer->getDataByPermName($curRouteName);
        $view->with('curPermData', $curPermData);
        //面包屑数据
        $breadcrumbList = $this->permSer->getBreadcrumbData(isset($curPermData['perm_id']) ? $curPermData['perm_id'] : 0);
        $view->with('breadcrumbList', $breadcrumbList);
        //当前选中菜单路由名
        //所有菜单的路由列表
        $menuRouteList = $this->menuSer->getMenuRouteList('admin');
        if (!in_array($curRouteName, $menuRouteList)){
            $parentPermData = $this->permSer->getParentPermData(isset($curPermData['perm_id']) ? $curPermData['perm_id'] : 0);
            $focusMenuRouteName = isset($parentPermData['name']) && $parentPermData['name'] ? $parentPermData['name'] : $curRouteName;
        }else{
            $focusMenuRouteName = $curRouteName;
        }
        $view->with('focusRouteName', $focusMenuRouteName);
        //当前菜单列表
        $treeMenuList = $this->menuSer->getHasPermTreeMenuList('admin');
        $view->with('treeMenuList', $treeMenuList);
    }
}